<?php
/**
 * 角色管理
 */
namespace app\backend\modules\user\controllers;

use app\backend\components\BaseController;
use Yii;
use app\models\base\User;
use app\models\base\BankAccount;
use yii\data\Pagination;
use app\backend\modules\user\models\Appuser;
use yii\helpers\Json;
use app\models\base\Role;

class RoleinfoController extends BaseController
{
	const SUC = 200;
    const ERR = 400;
    public function init()
    {
        parent::init();
        $this->data['js'] = [
            'global/plugins/layer/layer.js',
        ];
	}

	public function actionIndex()
	{
		$model = Role::find()->where(['disabled'=>0]);
		$pages = new Pagination([
				'totalCount'=>$model->count(),
				'defaultPageSize'=>20,
			]);
		$this->data['pages'] = $pages;
		$model->limit($pages->limit);
		$model->offset($pages->offset);
		$role = $model->all();
		$this->data['role'] = $role;
		return $this->render('index',$this->data);
	}

	public function actionAdd()
	{
		$attributes = Yii::$app->request->post('Role');
		if($attributes){
			$attributes['create_time'] = time();
			$model = new Role;
			$model->attributes = $attributes;
			$submit = $model->save()?200:500;
			$this->data['submit'] = $submit;
		}
		$this->data['action'] = 'add';
		return $this->render('add',$this->data);
	}

	//编辑角色
    public function actionUpdate()
    {
        $id = Yii::$app->request->get('id');
        $attributes = Yii::$app->request->post('Role');
        if($attributes){
            $attributes['last_modify'] = time();
            $model = Role::findOne($id);
            $model->attributes = $attributes;
            $submit = $model->save()?200:500;
            $this->data['submit'] = $submit;
            // $this->refresh('&ref_sub='.$submit);
        }
        $this->data['role'] = Role::findOne($id);
        $this->data['action'] = 'update&id='.$id;
        return $this->render('update',$this->data);
    }

    public function actionDelete()
    {
    	$id = Yii::$app->request->get('id');
    	$model = Role::findOne($id);
    	$model->disabled = 1;
    	$model->last_modify = time();
    	$submit = $model->save()?200:400;
    	if($submit == 200){
    		return json_encode(['code'=>200,'msg'=>'删除成功']);
    	}else{
    		return json_encode(['code'=>400,'msg'=>'删除失败']);
    	}
    }
}